﻿@{
    System.Collections.IList list = @Model as System.Collections.IList;
    
}

<style>
table 
{
    width:100%;
    border-collapse: collapse;
}
th, td
{
    font-size: 12px;
    padding: 10px;
    border: 1px solid #cccccc;
}
</style>

<table class="ui-widget ui-widget-content ui-corner-all">
    <tr>
    @{
        ModelMetadata _modelMetadataHeader = ModelMetadataProviders.Current.GetMetadataForType(null, list.GetType().GetGenericArguments()[0]);

        foreach (var prop in _modelMetadataHeader.Properties.Where(pm => pm.ShowForEdit))
        {
            <th class="ui-state-default ui-th-column ui-th-ltr">
                @prop.GetDisplayName()
            </th>
        }
        <th class="ui-state-default ui-th-column ui-th-ltr">Actions</th>
    }
    </tr>
    @for(int i = 0; i < list.Count; i++)
    {
        ModelMetadata _modelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => list[i], list[i].GetType());
        string keyId = "";
        int keyPosition = -1;
        int indexPosition = 0;
        <tr class='ui-widget-content'>
        @foreach (var prop in _modelMetadata.Properties.Where(pm => pm.ShowForEdit))
        {
            if (keyPosition < 0 && (System.ComponentModel.DataAnnotations.KeyAttribute)prop.ContainerType.GetProperty(@prop.PropertyName).GetCustomAttributes(typeof(System.ComponentModel.DataAnnotations.KeyAttribute), true).FirstOrDefault() != null)
            {
                keyId = @prop.Model.ToString();
                keyPosition = indexPosition;
            }
            else if (keyPosition == indexPosition)
            {
                keyId = @prop.Model.ToString();
            }
            <td>@prop.Model</td>
            indexPosition++;
        }
        <td style='width:150px;'>
            @Html.ActionLink("Edit", "Edit", new { id = keyId }) |
            @Html.ActionLink("Details", "Details", new { id = keyId }) |
            @Html.ActionLink("Delete", "Delete", new { id = keyId })
        </td>
        </tr>
    }
</table>